/* model */
import { 
  SelectUserComponentUser,
  BaseSelectUserResult,
  SelectUserComponentDepartment,
  SelectUserComponentRole,
  SelectUserComponentServiceProvider
} from '@src/component/common/BaseSelectUser/model'
import { BaseSelectUserSelectedItemType } from '@src/component/common/BaseSelectUser/types'
/* util */
import { isNotArray } from '@src/util/type'
import { 
  isDepartmentSelectType, 
  isRoleSelectType, 
  isServiceProviderSelectType, 
  isTagSelectType, 
  isUserSelectType 
} from '@src/component/common/BaseSelectUser/util/selected'

/** 
 * @description 从已选择列表中构建获取选人组件数据返回结果
 * -- 根据数据的不同类型区分存放的不同属性。
*/
function getBaseSelectUserResultBySelectUserItems(items: BaseSelectUserSelectedItemType[]): BaseSelectUserResult {
  
  if (isNotArray(items)) {
    items = []
  }
  
  let users: SelectUserComponentUser[] = []
  let departments: SelectUserComponentDepartment[] = []
  let roles: SelectUserComponentRole[] = []
  let serviceProviders: SelectUserComponentServiceProvider[] = []
  
  let item: BaseSelectUserSelectedItemType
  
  for (let i = 0; i < items.length; i++) {
    
    item = items[i]
    
    if (isDepartmentSelectType(item)) {
      departments.push(item as SelectUserComponentDepartment)
    }
    
    if (isTagSelectType(item)) {
      departments.push(item as SelectUserComponentDepartment)
    }
    
    if (isRoleSelectType(item)) {
      roles.push(item as SelectUserComponentRole)
    }
    
    if (isServiceProviderSelectType(item)) {
      serviceProviders.push(item as SelectUserComponentServiceProvider)
    }
    
    if (isUserSelectType(item)) {
      users.push(item as SelectUserComponentUser)
    }
    
  }
  
  return new BaseSelectUserResult(users, departments, roles, serviceProviders, items)
}

export {
  isDepartmentSelectType,
  getBaseSelectUserResultBySelectUserItems
}
